//
//  FireView.m
//  MyPopStar
//
//  Created by tarena on 16/1/12.
//  Copyright © 2016年 ssnb. All rights reserved.
//

#import "FireView.h"

@interface FireView ()
@end
@implementation FireView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self mInit];
    }
    return self;
}
-(void)mInit{
    self.emitterLayer.emitterPosition = CGPointMake(self.bounds.size.width/2.f, self.bounds.size.height/2.f);
    CGSize viewSize= self.layer.bounds.size;
    self.emitterLayer.emitterPosition = CGPointMake(viewSize.width / 2.f,viewSize.height);
    self.emitterLayer.emitterSize     = CGSizeMake(viewSize.width/2, 0);
    
    self.emitterLayer.emitterMode     = kCAEmitterLayerOutline;
    self.emitterLayer.emitterShape    = kCAEmitterLayerLine;
    self.emitterLayer.renderMode      = kCAEmitterLayerAdditive;
    self.emitterLayer.seed            = (arc4random()%100)+1;
    
}

-(void)fireEmmiter{
    self.emitterLayer.emitterCells =[NSArray arrayWithObject:[self spark]];
    CABasicAnimation *heartsBurst = [CABasicAnimation animationWithKeyPath:@"emitterCells.heart.birthRate"];
    heartsBurst.fromValue		= [NSNumber numberWithFloat:150.0];
    heartsBurst.toValue			= [NSNumber numberWithFloat:  0.0];
    heartsBurst.duration		= 5.0;
    heartsBurst.timingFunction	= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    [self.emitterLayer addAnimation:heartsBurst forKey:@"heartsBurst"];
}
-(CAEmitterCell *)rocket{
   CAEmitterCell *rocket= [CAEmitterCell emitterCell];
    
    rocket.birthRate		= 1.0;//产生一个
    rocket.emissionRange	= 0.25 * M_PI;  // some variation in angle
    rocket.velocity			= 380;//速度
    rocket.velocityRange	= 100;//速度偏差
    rocket.yAcceleration	= 75;//y方向加速度
    rocket.lifetime			= 1.02;	// we cannot set the birthrate < 1.0 for the burst
    
    rocket.contents			= (id) [[UIImage imageNamed:@"ring"] CGImage];
    rocket.scale			= 0.2;//缩放大小
    rocket.color			= [[UIColor redColor] CGColor];
    rocket.greenRange		= 1.0;		// different colors//三原色的容差
    rocket.redRange			= 1.0;
    rocket.blueRange		= 1.0;
    rocket.spinRange		= M_PI;		// slow spin//旋转
    return  rocket;
}

-(CAEmitterCell *)burst{
    CAEmitterCell* burst = [CAEmitterCell emitterCell];
    
    burst.birthRate			= 1.0;		// at the end of travel
    burst.velocity			= 0;
    burst.scale				= 2.5;
    burst.redSpeed			=-1.5;		// shifting
    burst.blueSpeed			=+1.5;		// shifting
    burst.greenSpeed		=+1.0;		// shifting
    burst.lifetime			= 0.35;
    return burst;
}

-(CAEmitterCell *)spark{
    CAEmitterCell* spark = [CAEmitterCell emitterCell];
    
    spark.birthRate			= 400;
    spark.velocity			= 125;
    spark.emissionRange		= 2* M_PI;	// 360 deg
    spark.yAcceleration		= 75;		// gravity
    spark.lifetime			= 3;
    
    spark.contents			= (id) [[UIImage imageNamed:self.fireImgName] CGImage];
    spark.scaleSpeed		=-0.2;
    spark.greenSpeed		=-0.1;
    spark.redSpeed			= 0.4;
    spark.blueSpeed			=-0.1;
    spark.alphaSpeed		=-0.25;
    spark.spin				= 2* M_PI;
    spark.spinRange			= 2* M_PI;
    return spark;
}



-(void)show{
    
    CAEmitterCell* rocket = [self rocket];
    CAEmitterCell* burst = [self burst];
    CAEmitterCell* spark = [self spark];
    
    self.emitterLayer.emitterCells	= [NSArray arrayWithObject:rocket];
    rocket.emitterCells				= [NSArray arrayWithObject:burst];
    burst.emitterCells				= [NSArray arrayWithObject:spark];
    
}

-(void)hide{
//    [self removeFromSuperview];
    self.emitterLayer.emitterCells=nil;
}

@end
